/*
 * jxUtils Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.file;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jxUtils.check.CheckUtil;
import org.jxUtils.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxUtils
 * @package org.jxUtils.file
 * @date 31/05/2014 - 19:47:09
 */
public class SearchFiles {
	
	/**
	 * @param file
	 * @param mediaFiles
	 * @param extensions
	 */
	private static final void searchFiles(final File file, final List<MediaFile> mediaFiles, final List<String> extensions) {
	
		//
		if(file.isDirectory()) {
			
			final File[] files = file.listFiles();
			
			//
			if(files != null) {

				// 
				for(final File f : files) {
					
					searchFiles(f, mediaFiles, extensions);
				}
			}
			
		}else {
			
			//
			if(CheckUtil.isNullOrEmpty(extensions)) {
				
				mediaFiles.add(new MediaFile(file));
				
			}else {
				
				final String fileName = file.getName().toUpperCase();

				// 
				for(final String extensao : extensions) {

					// 
					if(fileName.endsWith(extensao)) {
						
						mediaFiles.add(new MediaFile(file));
						
						break;
					}
				}
			}
		}
	}
	
	/**
	 * @param directory
	 * @return {@code Map<Digest, List<File>>}
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 */
	public static final Map<String, List<File>> getDuplicateFiles(final File directory) throws IOException, NoSuchAlgorithmException {
	
		return new FindDuplicateFiles().getDuplicateFiles(directory);
	}
	
	/**
	 * @param directory
	 * @param fileFilter
	 * @return {@code Map<Digest, List<File>>}
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 */
	public static final Map<String, List<File>> getDuplicateFiles(final File directory, final FileFilter fileFilter) throws IOException, NoSuchAlgorithmException {
	
		return new FindDuplicateFiles().getDuplicateFiles(directory, fileFilter);
	}
	
	/**
	 * @param files
	 * @return {@code Map<Digest, List<File>>}
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 */
	public static final Map<String, List<File>> getDuplicateFiles(final List<File> files) throws IOException, NoSuchAlgorithmException {
	
		return new FindDuplicateFiles().getDuplicateFiles(files);
	}
	
	/**
	 * @param directory
	 * @return {@code List<MediaFile>}
	 * @throws IOException
	 */
	public static final List<MediaFile> getMediaFiles(final File directory) throws IOException {
	
		return getMediaFiles(directory, null);
	}
	
	/**
	 * @param directory
	 * @param extensions
	 * @return {@code List<MediaFile>}
	 * @throws IOException
	 */
	public static final List<MediaFile> getMediaFiles(final File directory, final List<String> extensions) throws IOException {
	
		final List<MediaFile> mediaFiles = new ArrayList<>();
		
		search(directory, mediaFiles);
		
		return mediaFiles;
	}
	
	/**
	 * @param directory
	 * @param mediaFiles
	 * @throws IOException
	 */
	public static final void search(final File directory, final List<MediaFile> mediaFiles) throws IOException {
	
		search(directory, mediaFiles, null);
	}
	
	/**
	 * @param directory
	 * @param mediaFiles
	 * @param extensions
	 * @throws IOException
	 */
	public static final void search(final File directory, final List<MediaFile> mediaFiles, final List<String> extensions) throws IOException {
	
		//
		if( !directory.exists()) {
			
			throw new IOException(I18N.file().directoryNotExists(directory.getAbsolutePath()));
		}
		
		//
		if( !directory.isDirectory()) {
			
			throw new IOException(I18N.file().pathIsNotDirectory(directory.getAbsolutePath()));
		}
		
		searchFiles(directory, mediaFiles, extensions);
	}
}
